package com.wufun.overseas.adapters;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wufun.baselibrary.download.CommonDownloadTask;
import com.wufun.baselibrary.download.UtilsMy;
import com.wufun.overseas.R;
import com.wufun.overseas.common.TabTypeInterface;
import com.wufun.overseas.models.db.DownLoadModel;
import com.wufun.overseas.tools.DownloadManager;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by tangbl on 2017/12/9 0009.
 */

public class DownloadManagerAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<List<DownLoadModel>> mDataMap;
    private AdapterInterface mAdapterInterface;
    private groupViewHolder groupHolder;
    private childViewHolder childHolder;
    private String historyTitle, ingTitle;
    private Uri uri;

    public DownloadManagerAdapter(Context context, List<List<DownLoadModel>> mDataMap
            , AdapterInterface mAdapterInterface) {
        this.context = context;
        this.mDataMap = mDataMap;
        this.mAdapterInterface = mAdapterInterface;
        historyTitle = context.getResources().getString(R.string.download_history_title_str);
        ingTitle = context.getResources().getString(R.string.download_ing_title_str);
        uri = Uri.parse("res:///" + R.mipmap.localfile);
    }

    @Override
    public int getGroupCount() {
        return mDataMap.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (mDataMap.get(groupPosition) != null)
            return mDataMap.get(groupPosition).size();
        else
            return 0;
    }

    @Override
    public List<DownLoadModel> getGroup(int groupPosition) {
        return mDataMap.get(groupPosition);
    }

    @Override
    public DownLoadModel getChild(int groupPosition, int childPosition) {
        return mDataMap.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_download_title_view, null);
            groupHolder = new groupViewHolder(convertView);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (groupViewHolder) convertView.getTag();
        }

        List<DownLoadModel> temp = mDataMap.get(groupPosition);
        String str = ingTitle;
        if (temp.size() > 0) {
            if (temp.get(0).getStatus() == CommonDownloadTask.STATUS_FINISH) {
                str = historyTitle + "(" + temp.size() + ")";
                groupHolder.downClearTx.setVisibility(View.VISIBLE);
                groupHolder.downClearTx.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAdapterInterface.onAdapterItemGroupClick(v, groupPosition);
                    }
                });
            } else {
                str += "(" + temp.size() + ")";
                groupHolder.downClearTx.setVisibility(View.GONE);
            }
        }
        groupHolder.downTitleTx.setText(str);

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_download_view, null);
            childHolder = new childViewHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (childViewHolder) convertView.getTag();
        }

        final DownLoadModel temp = mDataMap.get(groupPosition).get(childPosition);
        childHolder.downNameTx.setText(temp.getGameName());
        if (temp.getType() == TabTypeInterface.TYPE_LOCAL) {
            childHolder.downImgIv.setImageURI(uri);
        } else {
            childHolder.downImgIv.setImageURI(temp.getGameImg());
        }

        if (temp.getProgress()==0) {
            DownLoadModel te = DownloadManager.getInstance().getDownloadByUrl(temp.getDownloadUrl());
            temp.setProgress(te.getProgress());
        }

        switch (temp.getStatus()) {
            case CommonDownloadTask.STATUS_ERROR:
                childHolder.btnOp.setText(context.getResources().getString(R.string.download_status_re_str));
                childHolder.btnOp.setBackgroundResource(R.drawable.recom_orange_butn);
                childHolder.lodingFailInfo.setText(context.getResources().getString(R.string.download_fail_str));
                childHolder.btnOp.setTextColor(context.getResources().getColor(R.color.app_main_color));
                childHolder.downProgressBar.setVisibility(View.GONE);
                childHolder.zipProgressBar.setVisibility(View.GONE);
                childHolder.appSize.setVisibility(View.GONE);
                childHolder.lodingInfo.setVisibility(View.GONE);
                childHolder.appTotalSize.setVisibility(View.VISIBLE);
                childHolder.lodingFailInfo.setVisibility(View.VISIBLE);
                break;
            case CommonDownloadTask.STATUS_STOP:
                childHolder.btnOp.setText(context.getResources().getString(R.string.download_status_goon_str));
                childHolder.lodingInfo.setText(context.getResources().getString(R.string.download_status_stop_str));
                childHolder.btnOp.setBackgroundResource(R.drawable.recom_blue_butn);
                childHolder.btnOp.setTextColor(context.getResources().getColor(R.color.app_blue_color));
                childHolder.downProgressBar.setVisibility(View.VISIBLE);
                childHolder.zipProgressBar.setVisibility(View.GONE);
                childHolder.appSize.setVisibility(View.VISIBLE);
                childHolder.lodingInfo.setVisibility(View.VISIBLE);
                childHolder.appTotalSize.setVisibility(View.GONE);
                childHolder.lodingFailInfo.setVisibility(View.GONE);
                break;
            case CommonDownloadTask.STATUS_START:
            case CommonDownloadTask.STATUS_PROGRESS:
                // appsize  lodinginfo
                childHolder.appSize.setText(UtilsMy.FormatFileSize(temp.getProgress() * temp.getTotalSize() / 100)
                        + "/" + UtilsMy.FormatFileSize(temp.getTotalSize()));
                childHolder.btnOp.setText(context.getResources().getString(R.string.download_status_stop_str));
                childHolder.lodingInfo.setText(temp.getSpeed() == null ? "0KB" : temp.getSpeed() + "/s");
                childHolder.btnOp.setBackgroundResource(R.drawable.recom_blue_butn);
                childHolder.btnOp.setTextColor(context.getResources().getColor(R.color.app_blue_color));
                childHolder.downProgressBar.setVisibility(View.VISIBLE);
                childHolder.zipProgressBar.setVisibility(View.GONE);
                childHolder.appSize.setVisibility(View.VISIBLE);
                childHolder.lodingInfo.setVisibility(View.VISIBLE);
                childHolder.appTotalSize.setVisibility(View.GONE);
                childHolder.lodingFailInfo.setVisibility(View.GONE);
                break;
            case CommonDownloadTask.STATUS_FINISH:
                childHolder.downProgressBar.setVisibility(View.GONE);
                childHolder.zipProgressBar.setVisibility(View.GONE);
                childHolder.appSize.setVisibility(View.GONE);
                childHolder.lodingInfo.setVisibility(View.GONE);
                childHolder.appTotalSize.setVisibility(View.VISIBLE);
                childHolder.lodingFailInfo.setVisibility(View.GONE);
                childHolder.btnOp.setText(context.getResources().getString(R.string.download_status_open_str));
                childHolder.btnOp.setBackgroundResource(R.drawable.recom_blue_butn);
                childHolder.btnOp.setTextColor(context.getResources().getColor(R.color.app_blue_color));
                childHolder.appTotalSize.setText(UtilsMy.FormatFileSize(temp.getTotalSize()));
                break;
            case CommonDownloadTask.STATUS_STSRT_ZIP:
            case CommonDownloadTask.STATUS_ZIP_PROGRESS:
                // appsize  lodinginfo
                childHolder.btnOp.setText(context.getResources().getString(R.string.zip_str));
                childHolder.appSize.setText(UtilsMy.FormatFileSize(temp.getProgress() * temp.getTotalSize() / 100)
                        + "/" + UtilsMy.FormatFileSize(temp.getTotalSize()));
                childHolder.lodingInfo.setText(context.getResources().getString(R.string.zip_str));
                childHolder.btnOp.setBackgroundResource(R.drawable.recom_gray_butn);
                childHolder.btnOp.setTextColor(context.getResources().getColor(R.color.mg_forum_avatar_round_stroke_color));
                childHolder.downProgressBar.setVisibility(View.VISIBLE);
                childHolder.zipProgressBar.setVisibility(View.GONE);
                childHolder.appSize.setVisibility(View.VISIBLE);
                childHolder.lodingInfo.setVisibility(View.VISIBLE);
                childHolder.appTotalSize.setVisibility(View.GONE);
                childHolder.lodingFailInfo.setVisibility(View.GONE);
                break;
            case CommonDownloadTask.STATUS_ZIP_FAIL:
                childHolder.btnOp.setText(context.getResources().getString(R.string.zip_fail_str));
                childHolder.btnOp.setBackgroundResource(R.drawable.recom_orange_butn);
                childHolder.btnOp.setTextColor(context.getResources().getColor(R.color.app_main_color));
                childHolder.lodingFailInfo.setText(context.getResources().getString(R.string.zip_fail_str));
                childHolder.downProgressBar.setVisibility(View.GONE);
                childHolder.zipProgressBar.setVisibility(View.GONE);
                childHolder.appSize.setVisibility(View.GONE);
                childHolder.lodingInfo.setVisibility(View.GONE);
                childHolder.appTotalSize.setVisibility(View.VISIBLE);
                childHolder.lodingFailInfo.setVisibility(View.VISIBLE);
                break;
            default:
                childHolder.btnOp.setText(context.getResources().getString(R.string.download_status_start_str));
                childHolder.btnOp.setBackgroundResource(R.drawable.recom_blue_butn);
                childHolder.btnOp.setTextColor(context.getResources().getColor(R.color.app_blue_color));
                childHolder.downProgressBar.setVisibility(View.GONE);
                childHolder.zipProgressBar.setVisibility(View.GONE);
                childHolder.appSize.setVisibility(View.GONE);
                childHolder.lodingInfo.setVisibility(View.GONE);
                childHolder.appTotalSize.setVisibility(View.VISIBLE);
                childHolder.lodingFailInfo.setVisibility(View.GONE);
                childHolder.appTotalSize.setText(UtilsMy.FormatFileSize(temp.getTotalSize()));
                break;
        }

//        childHolder.btnOp.setEnabled(true);

        if (childHolder.downProgressBar.getVisibility() == View.VISIBLE)
            childHolder.downProgressBar.setProgress(temp.getProgress());

        if (childHolder.downProgressBar.getVisibility() == View.VISIBLE)
            childHolder.zipProgressBar.setProgress(temp.getProgress());

        childHolder.rLayoutRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapterInterface.onAdapterItemChildClick(v, groupPosition, childPosition);
            }
        });

        childHolder.btnOp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapterInterface.onAdapterItemChildClick(v, groupPosition, childPosition);
//                if (temp.getStatus() == CommonDownloadTask.STATUS_PROGRESS){
//                    childHolder.btnOp.setEnabled(false);
//                }
            }
        });

        childHolder.itemLl.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mAdapterInterface.onAdapterItemLongChildClick(v, groupPosition, childPosition);
                return true;
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public interface AdapterInterface {

        void onAdapterItemGroupClick(View view, int groupPosition);

        void onAdapterItemChildClick(View view, int groupPosition, int childPosition);

        void onAdapterItemLongChildClick(View view, int groupPosition, int childPosition);
    }

    static class groupViewHolder {
        @Bind(R.id.down_title_tx)
        TextView downTitleTx;
        @Bind(R.id.down_clear_tx)
        TextView downClearTx;

        groupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class childViewHolder {
        @Bind(R.id.down_img_iv)
        SimpleDraweeView downImgIv;
        @Bind(R.id.down_name_tx)
        TextView downNameTx;
        @Bind(R.id.appTotalSize)
        TextView appTotalSize;
        @Bind(R.id.loding_fail_info)
        TextView lodingFailInfo;
        @Bind(R.id.appSize)
        TextView appSize;
        @Bind(R.id.loding_info)
        TextView lodingInfo;
        @Bind(R.id.down_progressBar)
        ProgressBar downProgressBar;
        @Bind(R.id.zip_progressBar)
        ProgressBar zipProgressBar;
        @Bind(R.id.btnOp)
        TextView btnOp;
        @Bind(R.id.rLayoutRight)
        RelativeLayout rLayoutRight;
        @Bind(R.id.item_ll)
        LinearLayout itemLl;

        childViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
